package com.weilive.api.gate.json.handler;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.weilive.api.ServerErrors;
import com.weilive.api.gate.json.protocol.CSA_BlackList;
import com.weilive.api.gate.json.protocol.CSQ_BlackList;
import com.weilive.api.service.BlackSystem;
import com.weilive.core.protocol.json.JsonRequest;
import com.weilive.core.protocol.json.JsonRequestHandler;
import com.weilive.core.protocol.json.JsonResponse;
import com.weilive.core.utils.StringUtils;

@Service
public class CSQ_BlackList_JsonHandler extends JsonRequestHandler {
	@Autowired
	private BlackSystem blackSystem;

	public Class<?> getRequestType() {
		return CSQ_BlackList.class;
	}

	public Class<?> getResponseType() {
		return CSA_BlackList.class;
	}

	public JsonResponse handleRequest(JsonRequest request) throws Exception {

		CSQ_BlackList params = (CSQ_BlackList) request.getData();
		CSA_BlackList response = new CSA_BlackList();
		if (StringUtils.isNull(params.version)) {
			response.version = blackSystem.getVersion();
			response.blackList = blackSystem.getBlackList();
			return JsonResponse.create(response);
		} else {
			if (!blackSystem.getVersion().equals(params.version)) {
				response.version = blackSystem.getVersion();
				response.blackList = blackSystem.getBlackList();
				return JsonResponse.create(response);
			}
		}
		return createErrorResponse(ServerErrors.BLACK_NOT_UPDATE);
	}

}
